package com.example.fristwork

import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView

class BookAdapter(val bookList: List<Book>) : RecyclerView.Adapter<BookAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val bookImage: ImageView = view.findViewById(R.id.bookImage)
        val bookName: TextView = view.findViewById(R.id.bookName)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.book_item, parent, false)
        val viewHolder = ViewHolder(view)
        viewHolder.itemView.setOnClickListener {
            val position = viewHolder.adapterPosition
            val book = bookList[position]
            Toast.makeText(parent.context, "you clicked view ${book.name}", Toast.LENGTH_SHORT).show()
        }
        viewHolder.bookImage.setOnClickListener {
            val position = viewHolder.adapterPosition
            val book = bookList[position]
            Toast.makeText(parent.context, "you clicked image ${book.name}", Toast.LENGTH_SHORT).show()
        }
        return viewHolder
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val fruit = bookList[position]
        holder.bookImage.setImageResource(fruit.imageId)
        holder.bookName.text = fruit.name
    }

    override fun getItemCount() = bookList.size

}